using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using FairyGUI;
using JKFrame;

namespace ABA
{
    public class FGUIPkg : Singleton<FGUIPkg>
    {
        private readonly Dictionary<string, UIPackage> haveList = new Dictionary<string, UIPackage>();
        private Dictionary<string, Dictionary<string, UnityEngine.Object>> havePkgList = new Dictionary<string, Dictionary<string, UnityEngine.Object>>();

        public IEnumerator AddPackageCoroutine(string pkgName)
        {
            bool isPkgLoadOk = false;
            AddPackageAsync(pkgName, (pkg) => {
                isPkgLoadOk = true;
            });

            while (!isPkgLoadOk)
            {
                yield return null;
            }
        }

        private void AddToHaveAssetList(string pkgName, string assetName, UnityEngine.Object res){
            if(!havePkgList.ContainsKey(pkgName)){
                var ll = new Dictionary<string,UnityEngine.Object>();
                ll.Add(assetName, res);
                havePkgList.Add(pkgName, ll);
            }    
            else{
                if(!havePkgList[pkgName].ContainsKey(assetName))
                    havePkgList[pkgName].Add(assetName, res);
            }
        }

        private UIPackage __AddPackage_ResLoad__(string pkgName, TextAsset pkgDesc)
        {
            var pkg = UIPackage.AddPackage(pkgDesc.bytes, "", (string name, string extension, System.Type type, PackageItem item) => {
                var assetName = item.owner.name + "_" + name;
                switch (item.type)
                {                   
                    case PackageItemType.Atlas:
                        ResSystemYooAsset.Instance.LoadAssetAsync<Texture>(assetName, (Texture res) => {
                            //destroyMethod 指示返回的资源应该怎样释放。例如复制的资源实例可以用Destroy，共享的资源实例需要用Unload，不需要底层处理则传None。
                            item.owner.SetItemAsset(item, res, DestroyMethod.None);
                            AddToHaveAssetList(pkgName, assetName, res);
                        });
                        break;

                    case PackageItemType.Sound:
                        ResSystemYooAsset.Instance.LoadAssetAsync<AudioClip>(assetName, (AudioClip res) => {
                            //destroyMethod 指示返回的资源应该怎样释放。例如复制的资源实例可以用Destroy，共享的资源实例需要用Unload，不需要底层处理则传None。
                            item.owner.SetItemAsset(item, res, DestroyMethod.None);
                            AddToHaveAssetList(pkgName, assetName, res);
                        });
                        break;
                }
            });

            haveList.Add(pkgName, pkg);

            return pkg;
        }

        private UIPackage __AddPackage(string descAssetName)
        {
            string pkgName = descAssetName.Substring(0, descAssetName.LastIndexOf('_'));
            var pkgDesc = ResSystemYooAsset.Instance.LoadAsset<TextAsset>(descAssetName);
            var pkg = __AddPackage_ResLoad__(pkgName, pkgDesc);

            return pkg;
        }

        private void __AddPackageAsync(string descAssetName, Action<UIPackage> cb)
        {
            string pkgName = descAssetName.Substring(0, descAssetName.LastIndexOf('_'));
            ResSystemYooAsset.Instance.LoadAssetAsync<TextAsset>(descAssetName, (pkgDesc) => {
                var pkg = __AddPackage_ResLoad__(pkgName, pkgDesc);
                cb(pkg);
            });
        }

        // 所有ui包的资源都是现加载的，同步加载包的desc文件，异步加载asset
        public UIPackage AddPackage(string pkgName){
            UIPackage pkg;
            if(!haveList.ContainsKey(pkgName)){
                pkg = __AddPackage(pkgName + "_fui");
            }
            else{
                pkg = haveList[pkgName];
            }

            return pkg;
        }

        public void AddPackageAsync(string pkgName, Action<UIPackage> cb)
        {
            if (!haveList.ContainsKey(pkgName))
            {
                __AddPackageAsync(pkgName + "_fui", cb);
            }
            else
            {
                var pkg = haveList[pkgName];
                cb(pkg);
            }
        }
    }
}
